package lore.miniwechat.security;


import com.fasterxml.jackson.databind.ObjectMapper;
import lore.miniwechat.dto.BaseResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class LoreAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        BaseResponse response = new BaseResponse();
        response.setCode("0");
        response.setMessage("登录成功！");

        // 把对象转成json字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(response);

        httpServletResponse.setContentType("application/json;charset=utf-8");

        // 响应给浏览器
        httpServletResponse.getWriter().write(result);
    }
}
